package com.enhinck.dbutilweb.common.util;

import lombok.Data;
import org.apache.commons.lang.StringUtils;

import java.io.File;
import java.util.*;

@Data
public class FileArrangeUtil {
    private Set<String> filelist;

    public Set<String> getFilelist() {
        return filelist;
    }

    public Set<String> extSet = new HashSet<>();

    public void setFilelist(Set<String> filelist) {
        this.filelist = filelist;
    }

    private void init() {
        filelist = new HashSet<String>();
    }



    // 类型  时间 2018  2019  2020  2021

    //  java  jpg jpeg png bat sh exe zip jar sql md log  py


    // 文档  doc docx pdf xlxs xls   ppt  pptx md

    Map<String, List<File>> doc = new LinkedHashMap<>();


    private String[] docExts = new String[]{"doc", "docx", "pdf", "xlxs", "xls", "ppt", "pptx", "md", "txt"};

    public boolean isDoc(String ext) {
        for (String docExt : docExts) {
            if (docExt.equalsIgnoreCase(ext)) {
                return true;
            }
        }
        return false;
    }


    // 图片  jpg jpeg png ico

    Map<String, List<File>> image = new LinkedHashMap<>();

    private String[] imageExts = new String[]{"jpg", "jpeg", "png", "gif", "ico", "bmp"};

    public boolean isImage(String ext) {
        for (String docExt : imageExts) {
            if (docExt.equalsIgnoreCase(ext)) {
                return true;
            }
        }
        return false;
    }

    // 脚本 sh bat sql

    Map<String, List<File>> script = new LinkedHashMap<>();
    private String[] scriptExts = new String[]{"sh", "bat", "sql"};

    public boolean isScript(String ext) {
        for (String docExt : scriptExts) {
            if (docExt.equalsIgnoreCase(ext)) {
                return true;
            }
        }
        return false;
    }


    // 安装包  exe  zip rar jar
    Map<String, List<File>> exe = new LinkedHashMap<>();

    private String[] exeExts = new String[]{"exe", "zip", "rar", "jar"};

    public boolean isExe(String ext) {
        for (String docExt : exeExts) {
            if (docExt.equalsIgnoreCase(ext)) {
                return true;
            }
        }
        return false;
    }

    // 代码 py java

    Map<String, List<File>> source = new LinkedHashMap<>();
    private String[] sourceExts = new String[]{"py", "java"};

    public boolean isSource(String ext) {
        for (String docExt : sourceExts) {
            if (docExt.equalsIgnoreCase(ext)) {
                return true;
            }
        }
        return false;
    }

    private void getFiles(final String filePath,FileProcessFactory fileProcessFactory, final String... exts) {
        File root = new File(filePath);
        File[] files = root.listFiles();
        for (File file : files) {
            if (file.isDirectory()) {
                /*
                 * 递归调用
                 */
                getFiles(file.getAbsolutePath(),fileProcessFactory, exts);
                // filelist.add(file.getAbsolutePath());
                // System.out.println("显示"+filePath+"下所有子目录及其文件"+file.getAbsolutePath());
            } else {
                // System.out.println("显示"+filePath+"下所有子目录"+file.getAbsolutePath());
                if (exts.length == 0) {
                    String fileName = file.getName();
                    fileProcessFactory.processFile(file);
                    if (fileName.contains(".")) {
                        String ext = StringUtils.substringAfterLast(fileName, ".");
                        extSet.add(ext);
                    }
                } else {
                    for (String ext : exts) {
                        if (file.getName().endsWith(ext)) {
                            filelist.add(file.getAbsolutePath());
                        }
                    }
                }
            }
        }
    }

    public static void main(String[] args) {

        FileProcessFactory fileProcessFactory = new FileProcessFactory();

        FileArrangeUtil fileArrangeUtil = new FileArrangeUtil();
        fileArrangeUtil.init();
        fileArrangeUtil.getFiles("/Users/huenbin/work/待整理/",fileProcessFactory);

        fileProcessFactory.copyFileTo("/Users/huenbin/work/整理完毕");

    }
}
